﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CommunityPanel:MonoBehaviour
{
    List<CommunityItem> m_Items = new List<CommunityItem>();
    public List<CommunityItem> items => m_Items;

    public Transform itemsParent;
    public CommunityItem itemPref;

    public Transform foot;

    ZUIAnimTools m_ZUITools;
    ZUIAnimTools UIAnimTools
    {
        get
        {
            if (m_ZUITools == null)
            {
                m_ZUITools = gameObject.AddComponent<ZUIAnimTools>();
                //m_ZUITools.InitCurve(AnimationCurve.)
            }
            return m_ZUITools;
        }
    }


    void Awake()
    {
        itemPref.gameObject.SetActive(false);
    }

    public void Init(List<UserMoment> userMoments,bool anim = true)
    {
        UnityFunctions.ClearList(m_Items);

        userMoments.Sort((a, b) => b.moment.time.CompareTo(a.moment.time));

        if(anim)
            Add(userMoments);
        else
        {
            AddMoments(userMoments);
        }
    }

    public void Add(List<UserMoment> userMoments)
    {
        StartCoroutine(IEAddMoments(userMoments));
    }

    IEnumerator IEAddMoments(List<UserMoment> userMoments)
    {
        //userMoments.Reverse();

        int newCount = 0;
        for (int i = 0; i < userMoments.Count; i++)
        {
            //if (userMoments[i].userId == MGame.instance.)

            if (m_Items.Find(e => e.userMoment.moment.momentId == userMoments[i].moment.momentId
            && e.userMoment.userId == userMoments[i].userId) != null)
                continue;

            var newItem = Instantiate(itemPref, itemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(userMoments[i]);

            //if (i > 0)
            {
                UIAnimTools.SetImgTxtAlpha(newItem.GetComponent<RectTransform>(), 0);
                UIAnimTools.UIAlpha(newItem.GetComponent<RectTransform>(), 1, 0.4f);
            }
            //newItem.transform.SetAsFirstSibling();
            if (foot)
                foot.transform.SetAsLastSibling();

            LayoutRebuilder.ForceRebuildLayoutImmediate(itemsParent.GetComponent<RectTransform>());

            m_Items.Add(newItem);
            newCount++;
            yield return new WaitForSeconds(0.1f);
        }
       // if(newCount > 0)
       //     StartCoroutine(UnityFunctions.AdjustItemsLayout(transform));
    }

    void AddMoments(List<UserMoment> userMoments)
    {
        int newCount = 0;
        for (int i = 0; i < userMoments.Count; i++)
        {
            //if (userMoments[i].userId == MGame.instance.)

            if (m_Items.Find(e => e.userMoment.moment.momentId == userMoments[i].moment.momentId
            && e.userMoment.userId == userMoments[i].userId) != null)
                continue;

            var newItem = Instantiate(itemPref, itemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(userMoments[i]);

            //newItem.transform.SetAsFirstSibling();
            if (foot)
                foot.transform.SetAsLastSibling();

            m_Items.Add(newItem);
            newCount++;
        }
    }

    void OnEnable()
    {
        //StartCoroutine(UnityFunctions.AdjustItemsLayout(transform));
    }

    void Update()
    {

    }
}
